package com.xbongbong.saas.analytical.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.saas.analytical.SaasValidateAnalyticalService;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.competitor.CompetitorAnalysisEntity;
import com.xbongbong.saas.enums.CompetitorErrorCodeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CompetitorAnalysisEnum;
import com.xbongbong.saas.model.CompetitorAnalysisModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author: yingjf
 * @date: 2022/9/14 16:24
 * @description:
 */
@Slf4j
@Service("competitorAnalysisValidateAnalyticalServiceImpl")
public class CompetitorAnalysisValidateAnalyticalServiceImpl implements SaasValidateAnalyticalService {

    @Resource
    private CompetitorAnalysisModel competitorAnalysisModel;

    @Override
    public List<Integer> type() {
        return Arrays.asList(XbbRefTypeEnum.COMPETITOR_ANALYSIS.getCode());
    }

    @Override
    public void analyticalData(ValidateDataDTO validateDataDTO) throws XbbException {
        beforeSave(validateDataDTO);
    }

    public void beforeSave(ValidateDataDTO validateDataDTO) throws XbbException {
        String corpid = validateDataDTO.getCorpid();
        JSONObject data = validateDataDTO.getData();
        Long competitorId = data.getJSONArray(CompetitorAnalysisEnum.COMPETITOR_NAME_HIDE.getAttr()).getJSONObject(0).getLong(StringConstant.ID);
        Long customerId = data.getJSONArray(CompetitorAnalysisEnum.CUSTOMER_NAME_HIDE.getAttr()).getJSONObject(0).getLong(StringConstant.ID);
        Long opportunityId = data.getJSONArray(CompetitorAnalysisEnum.OPPORTUNITY_NAME_HIDE.getAttr()).getJSONObject(0).getLong(StringConstant.ID);
        Map<String,Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("oppotunityId",opportunityId);
        params.put("competitorId",competitorId);
        params.put("customerId",customerId);
        params.put("corpid",corpid);
        params.put("del",0);
        List<CompetitorAnalysisEntity> competitorAnalysisEntityList = competitorAnalysisModel.findEntitys(params);
        if(CollectionsUtil.isNotEmpty(competitorAnalysisEntityList) && competitorAnalysisEntityList.size()>0){
            // 机会和竞争对手已经关联
            throw new XbbException(CompetitorErrorCodeEnum.API_ERROR_330001.getCode(), CompetitorErrorCodeEnum.API_ERROR_330001.getMsg());
        }
        params.clear();
        params.put("oppotunityId",opportunityId);
        params.put("corpid",corpid);
        params.put("del",0);
        Integer count = competitorAnalysisModel.getEntitysCount(params);
        if(count>=50){
            throw new XbbException(CompetitorErrorCodeEnum.API_ERROR_330002.getCode(), CompetitorErrorCodeEnum.API_ERROR_330002.getMsg());
        }

    }
}
